package com.java.test.system.templatemember;

import com.java.test.system.templatemember.dto.TemplateAddDTO;
import com.java.test.system.templatemember.dto.TemplateQueryDTO;
import com.java.test.system.templatemember.dto.TemplateUpdateDTO;
import com.java.test.system.templatemember.entity.LanguageEntity;
import com.java.test.system.templatemember.entity.TemplateEntity;
import com.java.test.system.templatemember.entity.TerminalEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 模板管理DAO
 *
 * @author lijie1
 * @date 2020/6/30 0030
 */
@Component
public class TemplateDAO {

    private JdbcTemplate jdbcTemplate;
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<TemplateEntity> getSelectByList(TemplateQueryDTO queryDTO) {
        String sql = "select * from sys_template";

        List<TemplateEntity> list = jdbcTemplate.query(sql,new TemplateEntity());

        return list;
    }

    /**
     * 获取数据BYName
     * @param addDTO
     * @return
     */
    public TemplateEntity getByGName(TemplateAddDTO addDTO) {
        String sql = "select * from sys_template where name = ? and version = ? and terminal = ? and area = ? and language = ? ";
        TemplateEntity info = null;
        try {
            info= jdbcTemplate.queryForObject(sql, new TemplateEntity(),
                    addDTO.getName(),addDTO.getVersion(),addDTO.getTerminal(),
                    addDTO.getAreaId(),addDTO.getLanguage());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 获取数据BYid
     * @param id
     * @return
     */
    public TemplateEntity getById(String id) {
        String sql = "select *  from sys_template where id = ?";
        TemplateEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new TemplateEntity(), id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

        return info;
    }

    /**
     * 保存
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public void insertSave(TemplateAddDTO addDTO) {
        String sql ="insert into sys_template(name,version,terminal,area,language,operation,createTime,status,url) " +
                "values (?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,addDTO.getName(),addDTO.getVersion(),addDTO.getTerminal(),addDTO.getAreaId(),
                addDTO.getLanguage(),addDTO.getOperation(),
                addDTO.getCreateTime(),addDTO.getStatus(),addDTO.getUrl());
    }

    /**
     * 判断当前数据是否存在
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public TemplateEntity getByKeyExcludeId(TemplateUpdateDTO updateDTO) {
        String sql = "select * from sys_template where name = ? and version = ? and terminal = ? and area = ? and language = ? and id <> ?";
        TemplateEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new TemplateEntity(),updateDTO.getName(),updateDTO.getVersion(),
                    updateDTO.getTerminal(),updateDTO.getAreaId(),updateDTO.getLanguage(),updateDTO.getId());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 修改
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public void updateById(TemplateUpdateDTO updateDTO) {
        String sql = "update sys_template " +
                "set  name = ? , version = ? , terminal = ? , area = ? , language = ? ,modifyTime = ?, status = ? , url = ? " +
                "where id = ?";

        jdbcTemplate.update(sql, updateDTO.getName(),updateDTO.getVersion(),updateDTO.getTerminal(),updateDTO.getAreaId(),
                updateDTO.getLanguage(), updateDTO.getModifyTime(),updateDTO.getStatus(),updateDTO.getUrl(),updateDTO.getId());
    }

    /**
     * 删除
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public void deleteById(String id) {
        String sql ="DELETE FROM sys_template WHERE id = ?";
        jdbcTemplate.update(sql,id);
    }

    /**
     * 语言类型查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<LanguageEntity> getLanguageListByIds(String ids) {
        String sql = "select * from sys_language where id in("+ids+")";
        List<LanguageEntity> list = new ArrayList<LanguageEntity>();
        try {
            list= jdbcTemplate.query(sql,new LanguageEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return list;
    }

    /**
     * 终端类型查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<TerminalEntity> getTerminalListByIds(String ids) {
        String sql = "select * from sys_terminal where id in("+ids+")";
        List<TerminalEntity> list = new ArrayList<TerminalEntity>();
        try {
            list= jdbcTemplate.query(sql,new TerminalEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return list;
    }
}
